<?php


namespace app\api\controller;
use app\api\logic\Merchant as MerchantLogic;

use app\api\logic\Posts as PostsLogic;
use think\App;
use think\Controller;
use think\facade\Request;
use think\facade\Session;

class Merchant extends Controller
{
    protected $failException = true;
    private $merchantLogic;
    private $postsLogic;

    public function __construct(App $app = null,MerchantLogic $merchantLogic,PostsLogic $postsLogic)
    {
        parent::__construct($app);
        $this->merchantLogic=$merchantLogic;
        $this->postsLogic=$postsLogic;
    }

    public function index(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Merchant.index');
        $result=$this->merchantLogic->index($data['id']);
        if($result){
            return json(['data'=>$result,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['data'=>[],'code'=>ERRNO['OTHERERR'],'msg'=>'Merchant does not exist']);
        }
    }

    public function my(){
        $id=Session::get('userId');
        $result=$this->merchantLogic->index($id);
        if($result){
            return json(['data'=>$result,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['msg'=>'Merchant does not exist or has expired','code'=>ERRNO['LoginNeed']]);
        }
    }

    public function login()
    {
        $data=$this->request->param();
        if($data['type']==2){
            $this->validate($data,'app\api\validate\Merchant.login_2');
        }else{
            $this->validate($data,'app\api\validate\Merchant.login_1');
        }
        $result=$this->merchantLogic->login($data);
        if($result['req']==1){
            if($result['scene']==2){
                return json(['data'=>['scene'=>2],'code'=>ERRNO['OK'],'msg'=>'ok']);
            }else{
                $userInfo=$this->loginInfo($result);
                return json(['data'=>$userInfo,'code'=>ERRNO['OK'],'msg'=>'ok']);
            }
        }else{
            return json(['msg'=>$result['msg'],'code'=>ERRNO['PARAMERR']]);
        }

    }


    private function loginInfo($result){
        $userInfo['id']=$result['id'];
//                $userInfo['username']=$result['username'];
        $userInfo['country']=$result['country'];
        $userInfo['country_name']=$result['country_name'];
        $userInfo['abbreviation']=$result['abbreviation'];
        $userInfo['city']=$result['city'];
        $userInfo['city_name']=$result['city_name'];
        $userInfo['airport_id']=$result['airport_id'];
        $userInfo['iata']=$result['iata'];
        $userInfo['terminal']=$result['terminal'];
        $userInfo['avatar']=$result['avatar'];
        $userInfo['type']=config('data.role_type.merchant');//1:商户2：用户
        $access_token=getToken($userInfo);
        $refresh_token=getToken($userInfo,2);
        $data=$userInfo;
        $data['scene']=$result['scene'];
        $data['access_token']=$access_token;
        $data['refresh_token']=$refresh_token;
        return $data;
    }



    public function logoff(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Merchant.logoff');
        $rep=$this->merchantLogic->logoff($data);
        if($rep['req']==1){
            return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'logoff success']);
        }else{
            return json(['msg'=>$rep['msg'],'code'=>ERRNO['OTHERERR']]);
        }
    }

    public function logout(){
        $this->merchantLogic->logout();
        return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    /**
     * 完善商户信息
     */
    public function register(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Merchant.complete');
        $result=$this->merchantLogic->complete($data);
        if($result){
            $userInfo=$this->loginInfo($result);
            return json(['data'=>$userInfo,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['msg'=>'complete error','code'=>ERRNO['OTHERERR']]);
        }
    }

    public function edit(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Merchant.edit');
        if(!isset($data['header_image']) || empty($data['header_image'])){
            return json(['msg'=>'Please upload a picture of the merchant\'s storefront','code'=>ERRNO['PARAMERR']]);
        }

        if($this->merchantLogic->edit($data)){
            return json(['data'=>'edit success','code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['msg'=>'edit error','code'=>ERRNO['OTHERERR']]);
        }
    }

    /**
     * 商户关联posts列表
     */
    public function lists(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Posts.lists');
        $params['id']=$data['id'];
        $info=$this->merchantLogic->getInfo(['id'=>$params['id']],['airport']);
        if(empty($info)){
            return json(['msg'=>'Merchant does not exist or has expired','code'=>ERRNO['LoginNeed']]);
        }
        $params['airport_id']=$info['airport'];
        $params['page']=$this->request->post('page',1);
        $params['pageSize']=$this->request->post('pageSize',10);

        $data=$this->postsLogic->lists($params);
        return json(['data'=>$data,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    /**
     * 我的post列表
     */
    public function myPosts(){
        $params['id']=Session::get('userId');
        $info=$this->merchantLogic->getInfo(['id'=>$params['id']],['airport']);
        if(empty($info)){
            return json(['msg'=>'Merchant does not exist or has expired','code'=>ERRNO['LoginNeed']]);
        }
        $params['page']=$this->request->post('page',1);
        $params['pageSize']=$this->request->post('pageSize',10);
        $params['airport_id']=$info['airport'];
        $data=$this->postsLogic->lists($params);
        return json(['data'=>$data,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function myLike(){
        $params['id']=Session::get('userId');
        $info=$this->merchantLogic->getInfo(['id'=>$params['id']],['airport']);
        if(empty($info)){
            return json(['msg'=>'Merchant does not exist or has expired','code'=>ERRNO['LoginNeed']]);
        }
        $params['page']=$this->request->post('page',1);
        $params['pageSize']=$this->request->post('pageSize',10);
        $params['airport_id']=$info['airport'];
        $data=$this->postsLogic->myLike($params);
        return json(['data'=>$data,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function avatar(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Merchant.avatar');
        if($this->merchantLogic->update($data)){
            return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'upload success']);
        }else{
            return json(['msg'=>'update error','code'=>ERRNO['OTHERERR']]);
        }
    }


}